package ink.xiaobaibai.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import ink.xiaobaibai.bo.DeviceOpenInfoBo;
import ink.xiaobaibai.bo.UserAndDep;
import ink.xiaobaibai.common.PageInfo;
import ink.xiaobaibai.dao.OzoneDeviceDao;
import ink.xiaobaibai.entity.OzoneDevice;
import ink.xiaobaibai.service.IAdminUserService;
import ink.xiaobaibai.service.IOzoneDeviceService;
import ink.xiaobaibai.vo.UserByDeviceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xiaobaibai
 * @since 2021-05-22
 */

@Service
public class OzoneDeviceServiceImpl extends ServiceImpl<OzoneDeviceDao, OzoneDevice> implements IOzoneDeviceService {

    @Autowired
    private IAdminUserService adminUserService;

    @Override
    public UserAndDep getAdminUserAndDepByDeviceId(Integer deviceId) {
        return this.baseMapper.getAdminUserAndDepByDeviceId(deviceId);
    }

    @Override
    public PageInfo<OzoneDevice> getListByAdmin(Integer pageNum, Integer pageSize, Integer adminUserId, Integer depId, String provinceCity, String status) {
        int limitStart = (pageNum - 1) * pageSize;
        int limitEnd = pageSize;
        List<Integer> userIds = null;
        if (depId != null) {
            userIds = this.adminUserService.getUserIdByDepId(depId);
        } else if (adminUserId != null) {
            userIds = Collections.singletonList(adminUserId);
        }
        List<OzoneDevice> list = this.baseMapper.getListByAdmin(limitStart, limitEnd, userIds, provinceCity != null ? provinceCity + "%" : null, status);
        Integer count = this.baseMapper.getListByAdminCount(userIds, provinceCity != null ? provinceCity + "%" : null, status);
        return new PageInfo<>(list, count, pageSize);
    }

    @Override
    public UserByDeviceVo getUserInfoByUserId(Integer userId) {
        return this.baseMapper.getUserInfoByUserId(userId);
    }

    @Override
    public DeviceOpenInfoBo getDeviceOpeInfo(Integer id) {
        return this.baseMapper.getDeviceOpeInfo(id);
    }

    @Override
    public boolean identicalDeviceDepByUserId(Integer userId, Integer deviceId) {
        return this.baseMapper.identicalDeviceDepByUserId(userId, deviceId) == 1;
    }

    @Override
    public boolean identicalDeviceDepByUserId(Integer userId, String deviceCode) {
        return this.baseMapper.identicalDeviceDepByUserIdCode(userId, deviceCode) == 1;
    }

    @Override
    public String getOxygenCodeByDeviceCode(String deviceCode) {
        return this.baseMapper.getOxygenCodeByDeviceCode(deviceCode);
    }

}
